package com.atqh.logistics.service.impl;


import com.atqh.logistics.domin.Custom;
import com.atqh.logistics.domin.Driver;
import com.atqh.logistics.domin.Mission;
import com.atqh.logistics.domin.Notice;
import com.atqh.logistics.mapper.DriverMapper;
import com.atqh.logistics.mapper.MissionMapper;
import com.atqh.logistics.mapper.NoticeMapper;
import com.atqh.logistics.utils.ErrorCode;
import com.atqh.logistics.utils.Result;
import com.atqh.logistics.utils.Vo.MissionAndCarAndCustom;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atqh.logistics.service.DriverService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *
 */
@Service
public class DriverServiceImpl extends ServiceImpl<DriverMapper, Driver>
        implements DriverService {

    private static final String salt = "lum!@#";
    @Autowired
    private DriverMapper driverMapper;
    @Autowired
    private MissionMapper missionMapper;
    @Autowired
    private NoticeMapper noticeMapper;


    @Override
    public Result login(String account, String password, HttpServletRequest req) {

        if (StringUtils.isBlank(account) || StringUtils.isBlank(password)) {
            return Result.Fail(ErrorCode.ACCOUNT_PWD_NOT_EXIST.getCode(), ErrorCode.ACCOUNT_PWD_NOT_EXIST.getMsg());
        }
        String psw = DigestUtils.md5Hex(password + salt);
        LambdaQueryWrapper<Driver> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Driver::getNumid, account).eq(Driver::getPassword, psw).last("limit 1");
        Driver driver = driverMapper.selectOne(queryWrapper);
        if (driver == null) {
            return Result.Fail(ErrorCode.ACCOUNT_PWD_NOT_EXIST.getCode(), ErrorCode.ACCOUNT_PWD_NOT_EXIST.getMsg());
        } else if (driver != null) {
            req.getSession().setAttribute("driver", driver);
        }
        return Result.success(driver);
    }

    @Override
    public Result logout(HttpServletRequest req) {

        Driver driver = (Driver) req.getSession().getAttribute("driver");
        if (driver != null) req.getSession().invalidate();
        return Result.success(null);
    }

    @Override
    public Result getOwnInfo(HttpServletRequest req) {

        LambdaQueryWrapper<Driver> queryWrapper = new LambdaQueryWrapper<>();
        Driver driver1 = (Driver) req.getSession().getAttribute("driver");
        queryWrapper.eq(Driver::getId, driver1.getId());
        Driver driver = driverMapper.selectOne(queryWrapper);
        if (driver == null) {
            return Result.Fail(ErrorCode.ACCOUNT_PWD_NOT_EXIST.getCode(), ErrorCode.ACCOUNT_PWD_NOT_EXIST.getMsg());
        }
        return Result.success(driver);
    }

    @Override
    public Result getOwnTask(HttpServletRequest req) {
        Driver driver1 = (Driver) req.getSession().getAttribute("driver");
        List<MissionAndCarAndCustom> missions = missionMapper.selectOwnTaskList(driver1.getId());
        return Result.success(missions);
    }

    @Override
    public Result UpdateOwnTask( int missionId) {
        LambdaQueryWrapper<Mission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Mission::getId, missionId);
        Mission mission = new Mission();
        mission.setSituation(1);
        missionMapper.update(mission, queryWrapper);
        return Result.success(null);
    }

    @Override
    public Result ViewNotice() {
        List<Notice> notices = noticeMapper.selectList(null);
        return Result.success(notices);
    }
}




